{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fbe539e6-1e8a-49e9-b2b9-5e5c528364b8",
   "metadata": {},
   "source": [
    "# Minimum and Maximum\n",
    "\n",
    "The minimum and maximum operators determine the smallest and largest input, respectively.\n",
    "Both functions receive two inputs. Each may be a fixed point number or a quantum register."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "286e60ea-b549-464d-a01b-85dd13b92577",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "#### Example 1: Two Quantum Variables Minimum\n",
    "\n",
    "This code example generates a quantum program that returns a minimum of two arguments.\n",
    "Both the left and right arguments are defined as quantum variables of size three."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7ceea4e-69b2-4f34-bfac-de55d23b9899",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:05.925981Z",
     "iopub.status.busy": "2024-05-07T13:22:05.925500Z",
     "iopub.status.idle": "2024-05-07T13:22:08.926013Z",
     "shell.execute_reply": "2024-05-07T13:22:08.925139Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    hadamard_transform,\n",
    "    prepare_int,\n",
    "    qfunc,\n",
    ")\n",
    "from classiq.qmod.symbolic import min\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    prepare_int(4, a)\n",
    "    allocate(3, b)\n",
    "    hadamard_transform(b)\n",
    "\n",
    "    res |= min(a, b)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ab2c7cd-fc78-47b7-ac0f-a48868fb9ebe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:08.930433Z",
     "iopub.status.busy": "2024-05-07T13:22:08.929894Z",
     "iopub.status.idle": "2024-05-07T13:22:13.751512Z",
     "shell.execute_reply": "2024-05-07T13:22:13.750789Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 4.0, 'b': 1.0, 'res': 1.0}: 136,\n",
       " {'a': 4.0, 'b': 7.0, 'res': 4.0}: 129,\n",
       " {'a': 4.0, 'b': 6.0, 'res': 4.0}: 126,\n",
       " {'a': 4.0, 'b': 4.0, 'res': 4.0}: 125,\n",
       " {'a': 4.0, 'b': 2.0, 'res': 2.0}: 122,\n",
       " {'a': 4.0, 'b': 3.0, 'res': 3.0}: 122,\n",
       " {'a': 4.0, 'b': 5.0, 'res': 4.0}: 122,\n",
       " {'a': 4.0, 'b': 0.0, 'res': 0.0}: 118]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"minimum_2vars_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4821f55d-ebfa-46fe-ba4a-353a7f853112",
   "metadata": {},
   "source": [
    "#### Example 2: Float and Quantum Variable Maximum\n",
    "\n",
    "This code example returns a quantum program with a maximum of two arguments.\n",
    "Here, the left arg is a fixed-point number $(11.1)_2$ (3.5),\n",
    "and the right arg is a quantum variable of size three."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7efdb898-a317-43d9-8915-5f4719a93217",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:13.755779Z",
     "iopub.status.busy": "2024-05-07T13:22:13.755089Z",
     "iopub.status.idle": "2024-05-07T13:22:13.774219Z",
     "shell.execute_reply": "2024-05-07T13:22:13.773439Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    hadamard_transform,\n",
    "    qfunc,\n",
    ")\n",
    "from classiq.qmod.symbolic import max\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate(3, a)\n",
    "    hadamard_transform(a)\n",
    "\n",
    "    res |= max(3.5, a)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "79376eb8-baaa-4bc8-b8df-15915e20d577",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:22:13.778657Z",
     "iopub.status.busy": "2024-05-07T13:22:13.777449Z",
     "iopub.status.idle": "2024-05-07T13:22:17.397835Z",
     "shell.execute_reply": "2024-05-07T13:22:17.397147Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 5.0, 'res': 5.0}: 135,\n",
       " {'a': 0.0, 'res': 3.5}: 131,\n",
       " {'a': 3.0, 'res': 3.5}: 130,\n",
       " {'a': 6.0, 'res': 6.0}: 129,\n",
       " {'a': 2.0, 'res': 3.5}: 127,\n",
       " {'a': 7.0, 'res': 7.0}: 122,\n",
       " {'a': 1.0, 'res': 3.5}: 115,\n",
       " {'a': 4.0, 'res': 4.0}: 111]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"maximum_float_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
